package com.estar.integaration.strategy;

import com.estar.integaration.entity.paymentPlanEntity.PurchaseOrderDetail;
import com.estar.integaration.entity.paymentPlanEntity.ReceiptComeLedger;
import com.estar.integaration.entity.paymentPlanEntity.SupplierAccountPeriod;
import lombok.AllArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * Author: YJL
 * Date: 2022/2/18 15:44
 * 未付全款-未入库-已到票 NNY
 **/
@AllArgsConstructor
public class StrategyNNY extends Strategy {


  Map<String, ReceiptComeLedger> receiptComeLedgerMap = null;
  Map<String, SupplierAccountPeriod> supplierAccountPeriodMap = null;

  @Override
  public Boolean setExpectDate(List<PurchaseOrderDetail> purchaseOrderDetailList,PurchaseOrderDetail purchaseOrderDetail,String type) {
    if(purchaseOrderDetailList == null ||
        purchaseOrderDetail == null||
        purchaseOrderDetail.getOrderNumber() == null)
        return false;
    if(receiptComeLedgerMap == null)
      return false;
    ReceiptComeLedger receiptComeLedger = receiptComeLedgerMap.get(purchaseOrderDetail.getOrderNumber());
    if(receiptComeLedger != null){//找到了发票到票台账
      String date = receiptComeLedger.getTicketComeTime();
      if(date == null){
        purchaseOrderDetailList.forEach(p -> p.setExpectPayDate("有到票台账但无日期"));
        return false;
      }
      SupplierAccountPeriod supplierAccountPeriod = supplierAccountPeriodMap.get(purchaseOrderDetail.getSupplierName());
      if(supplierAccountPeriod != null && supplierAccountPeriod.getAccountPeriod() != null){
        String newDate = dateAddAccountPeriod(date,supplierAccountPeriod.getRealNeedAddDay());
        purchaseOrderDetailList.forEach(
            p -> {
              p.setExpectPayDate(newDate);
              p.setNeedPayAmount(p.getPriceTaxGather());
              p.setType("NNY");
              p.setIfSpecial(false);
            }
        );
        return true;
      }else {
        purchaseOrderDetailList.forEach(po -> po.setExpectPayDate("未找到供应商账期"));
        return false;
      }
    }else {
      return false;
    }
  }
}
